<?php

// +----------------------------------------------------------------------
// | 分类控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\app\admin;

use app\common\event\SystemConfig as SystemConfigEvent;
use app\common\model\SystemConfig as SystemConfigModel;
use app\model\app\Video as VideoModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use think\facade\Url;
use app\controller\admin\Common;


class Video extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
    }

    public function index()
    {
        //读取规则列表
        $map = $this->_search($this->dbname);
        //            //显示页面
        $builder = new Table();
        $builder->model(VideoModel::class)
            ->where($map)
            ->order('id DESC')
            ->setSearchPostUrl('', '')
            ->buttonUpdate()
            ->keyId()
            ->keyText('title', '名称')
            ->keyText('desc', '描述')
            ->keyImageModel('cover', '封面')
            ->keyText('desc', '描述')
            ->actionUpdate();


        return $builder->fetch();
    }
 

    /**
     * 编辑分类
     * @access protected
     * @author 微尘 <yicmf@qq.com>
     */
    public function update()
    {
        if ($this->request->isPost()) { //提交表单
            if ($this->request->param('id')) {

                $data = VideoModel::where('id', $this->request->param('id'))->find();
                $info = $this->request->only(['title', 'video_id', 'cover_id', 'desc','video_url']);
                $data->save($info);
                $this->_returnSuccess('更新成功');
            } else {
                VideoModel::create($this->request->param());
                $this->_returnSuccess('新增成功');
            }
        } else {
            /* 获取分类信息 */
            $id = $this->request->get('id');
            $isEdit = $id ? true : false;
            if ($isEdit) {
                $data = VideoModel::where('id', $id)->find();
                if (!$data) {
                    $this->_returnError('分类不存在或者已经删除');
                }
            } else {
                $data = [];
                $data['user_id'] = $this->user['id'];
            }

            $builder = new Edit();
            $builder->title('视频管理')
                ->keyHidden('user_id')
                ->keyText('title', '名称', '后台区分', '', '')
                ->keyTextArea('desc', '描述', 'APP下面展示')
                ->keyText('video_url', '视频链接')
                ->keyImageModel('cover_id','封面')
//                ->keyAttachmentModel('video_id', '视频文件')
                ->data($data)
                ->buttonSubmit('', '保存');
            //                    ->buttonClose();
            return $builder->fetch();
        }
    }
}